﻿using CraftParameterConfig.Infrastructure.Common;
using CraftParameterConfig.Infrastructure.Common.Enums;
using CraftParameterConfig.Services.Interfaces;
using CraftParameterConfig.Services.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CraftParameterConfig.UI.TableConfig
{
    public partial class frmTableFieldConfig : Form
    {
        ITableFieldAuth tableFieldAuth;
        ITableFieldService tableFieldService;
        ILogInfo logInfo;
        UpdateTypeEnum updateType;
        List<TableIndexDTO> indexDTOs;
        List<TableDataDTO> dataDTOs;
        public frmTableFieldConfig()
        {
            InitializeComponent();
        }

        private void frmTableFieldConfig_Load(object sender, EventArgs e)
        {
            tableFieldAuth = ObjectFactory.GetObject("ITableFieldAuth") as ITableFieldAuth;
            tableFieldService = ObjectFactory.GetObject("ITableFieldService") as ITableFieldService;
            logInfo = ObjectFactory.GetObject("ILogInfo") as ILogInfo;

            tp_Detail.PageVisible = false;
            //初始化绑定数据源
            BindDataSource();
            btnSearch_Click(null, null);
        }
        private void BindDataSource()
        {
            DataTable dtNames = tableFieldService.GetNameTable();

            lookNameList.Properties.DisplayMember = "NAME";
            lookNameList.Properties.ValueMember = "GUIDID";
            lookNameList.Properties.DataSource = dtNames;
        }


        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (lookNameList.EditValue == null && sender != null)
            {
                MessageBox.Show("请先配置表名,谢谢!");
                return;
            }
            if (lookNameList.EditValue == null)
            {
                return;
            }
            var guidid = lookNameList.EditValue.ToString();
            DataTable dtIndex = tableFieldService.GetIndexTable(guidid);
            DataTable dtData = tableFieldService.GetDataTable(guidid);

            gcIndexList.DataSource = dtIndex;
            gcDataList.DataSource = dtData;
        }

        private void lookNameList_EditValueChanged(object sender, EventArgs e)
        {
            btnSearch_Click(null, null);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确定需要取消当前的操作吗?") == DialogResult.OK)
            {
                tp_Detail.PageVisible = false;
                tp_Summary.PageVisible = true;

                tc_Main.SelectedTabPage = tp_Summary;
            }
        }

        private void btnCommit_Click(object sender, EventArgs e)
        {
            if (txtIndexName.Properties.ReadOnly == true)
            {
                updateType = UpdateTypeEnum.Modify;
            }
            else
            {
                updateType = UpdateTypeEnum.Add;
            }
            string guidid = lookNameList.EditValue.ToString();
            //为了简化操作,每次只能提交索引字段或者值字段中的一个
            if (!string.IsNullOrEmpty(txtIndexName.Text))
            {
                if (comIndexDataType.SelectedIndex == -1 || comIndexDataCondition.SelectedIndex == -1)
                {
                    MessageBox.Show("请将索引字段信息补充完整,再进行提交");
                    return;
                }
                TableIndexDTO tableIndexDTO = new TableIndexDTO();
                tableIndexDTO.Name = txtIndexName.Text.Trim();
                tableIndexDTO.DataType = comIndexDataType.Text;
                tableIndexDTO.DataCondition = comIndexDataCondition.Text;
                tableIndexDTO.UpdateType = updateType;
                tableIndexDTO.MainId = guidid;

                if (updateType == UpdateTypeEnum.Modify)
                {
                    int orderIndex = Convert.ToInt32(txtIndexOrder.Text.Trim());
                    if (orderIndex <= 0 || orderIndex > indexDTOs.Count)
                    {
                        MessageBox.Show("指定的排序超出范围,请重新指定!");
                        return;
                    }
                    tableIndexDTO.Order_Index = orderIndex;
                    tableIndexDTO.ID = Convert.ToInt32(txtIndexID.Text);
                    var temp = indexDTOs.Where(t => t.Name == txtIndexName.Text.Trim()).First();
                    indexDTOs.Remove(temp);
                    indexDTOs.Insert(orderIndex - 1, tableIndexDTO);


                    //重新整理排序信息
                    foreach (var item in indexDTOs) 
                    {
                        if (item.Order_Index != indexDTOs.IndexOf(item) + 1)
                        {
                            item.UpdateType = updateType;
                            item.Order_Index = indexDTOs.IndexOf(item) + 1;
                        }
                    }
                }
                else
                {
                    if (indexDTOs.Where(t => t.Name == txtIndexName.Text).Count()>0)
                    {
                        MessageBox.Show("当前清单已包含此字段,请重新配置!");
                        return;
                    }
                    tableIndexDTO.Order_Index = indexDTOs.Count + 1;
                    indexDTOs.Add(tableIndexDTO);
                }
                gcIndexEdit.DataSource = indexDTOs;
                gcIndexEdit.RefreshDataSource();

                //重新初始化信息
                txtIndexName.Text = "";
                txtIndexName.Properties.ReadOnly = false;
                comIndexDataType.SelectedIndex = -1;
                comIndexDataCondition.SelectedIndex = -1;
                txtIndexOrder.Text = "";
                txtIndexOrder.Properties.ReadOnly = true;

            }
            else
            {
                if (comDataDataType.SelectedIndex == -1 || (!chkNeedBaseValue.Checked && !chkLHValue.Checked && !chkLH2Value.Checked))
                {
                    MessageBox.Show("请将值字段信息补充完整,再进行提交");
                    return;
                }
                if (dataDTOs.Where(t => t.Name == txtDataName.Text).Count() > 0)
                {
                    MessageBox.Show("当前清单已包含此字段,请重新配置!");
                    return;
                }
                TableDataDTO tableDataDTO = new TableDataDTO();
                tableDataDTO.Name = txtDataName.Text.Trim();
                tableDataDTO.DataType = comDataDataType.Text;
                tableDataDTO.NeedBaseValue = chkNeedBaseValue.Checked ? "TRUE" : "FALSE";
                tableDataDTO.NeedLHValue = chkLHValue.Checked ? "TRUE" : "FALSE";
                tableDataDTO.NeedLLHHValue = chkLH2Value.Checked ? "TRUE" : "FALSE";
                tableDataDTO.Order_Index = dataDTOs.Count + 1;
                tableDataDTO.MainId = guidid;
                tableDataDTO.UpdateType = UpdateTypeEnum.Add;
                dataDTOs.Add(tableDataDTO);

                gcDataEdit.DataSource = dataDTOs;
                gcDataEdit.RefreshDataSource();

                //重新初始化信息
                txtDataName.Text = "";
                comDataDataType.SelectedIndex = -1;
                chkNeedBaseValue.Checked = false;
                chkLHValue.Checked = false;
                chkLH2Value.Checked = false;
            }
        }

        private void btnRollBack_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确定需要回滚当前的操作吗?") == DialogResult.OK)
            {
                txtIndexName.Text = "";
                comIndexDataType.SelectedIndex = -1;
                comIndexDataCondition.SelectedIndex = -1;
                txtIndexOrder.Text = "";

                txtIndexName.Properties.ReadOnly = false;
                txtIndexOrder.Properties.ReadOnly = true;
                updateType = UpdateTypeEnum.None;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //同时执行新增索引字段,新增值字段,更新索引字段的功能
            List<TableIndexDTO> indexDTOsAdd = indexDTOs.Where(t => t.UpdateType == UpdateTypeEnum.Add).ToList();
            List<TableIndexDTO> indexDTOsModify = indexDTOs.Where(t => t.UpdateType == UpdateTypeEnum.Modify).ToList();
            List<TableDataDTO> dataDTOsAdd = dataDTOs.Where(t => t.UpdateType == UpdateTypeEnum.Add).ToList();

            //新增索引字段
            tableFieldService.AddIndex(indexDTOsAdd);
            //修改索引字段
            tableFieldService.UpdateIndex(indexDTOsModify);
            //新增值字段
            tableFieldService.AddData(dataDTOsAdd);

            MessageBox.Show("数据保存成功!");

            tp_Summary.PageVisible = true;
            tp_Detail.PageVisible = false;
            tc_Main.SelectedTabPage = tp_Summary;
            btnSearch_Click(null, null);
        }

        private void gcIndexEdit_DoubleClick(object sender, EventArgs e)
        {
            if (gvIndexEdit.SelectedRowsCount <= 0)
            {
                MessageBox.Show("请选择需要修改的索引条件!");
                return;
            }
            var data = gvIndexEdit.GetFocusedRow() as TableIndexDTO;
            if (data.UpdateType == UpdateTypeEnum.Add)
            {
                MessageBox.Show("选择数据是新增加的数据,请保存到数据库后再进行修改!");
                return;
            }
            txtIndexName.Properties.ReadOnly = true;
            txtIndexOrder.Properties.ReadOnly = false;
            txtIndexID.Text = data.ID.ToString();
            txtIndexName.Text = data.Name;
            comIndexDataType.Text = data.DataType;
            comIndexDataCondition.Text = data.DataCondition;
            txtIndexOrder.Text = data.Order_Index.ToString();
        }

        private void chkLHValue_CheckedChanged(object sender, EventArgs e)
        {
            if (chkLHValue.Checked)
            {
                chkLH2Value.Enabled = true;
            }
            else
            {
                chkLH2Value.Enabled = false;
                chkLH2Value.Checked = false;
            }
        }
        private void btnEdit_Click(object sender, EventArgs e)
        {
            //此处权限仅用于测试,不对实际情况负责
            if (!tableFieldAuth.GetAddAuth("SC"))
            {
                MessageBox.Show("您没有被权限编辑当前表的字段!");
                return;
            }

            var guidid = lookNameList.EditValue.ToString();
            indexDTOs = null;
            dataDTOs = null;

            DataTable dtIndex = tableFieldService.GetIndexTable(guidid);
            DataTable dtData = tableFieldService.GetDataTable(guidid);

            ObjectChange obc = new ObjectChange();
            indexDTOs = obc.ChangeTableToObject<TableIndexDTO>(dtIndex).ToList();
            dataDTOs = obc.ChangeTableToObject<TableDataDTO>(dtData).ToList();

            gcIndexEdit.DataSource = indexDTOs;
            gcDataEdit.DataSource = dataDTOs;

            txtIndexOrder.Properties.ReadOnly = true;
            labelMessage.Text = string.Format("您当前正在编辑{0}的数据!", lookNameList.Text);

            tp_Detail.PageVisible = true;
            tp_Summary.PageVisible = false;
            tc_Main.SelectedTabPage = tp_Detail;
        }
    }
}
